package com.zzptc.mzb.shucheng1130;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ScrollView;
import android.widget.TextView;

import com.zzptc.mzb.shucheng1130.Utils.ProcessText;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.FloatBuffer;

public class Main2Activity extends AppCompatActivity {
    ScrollView scroll;
    Button getPre,getNext;
    TextView readtxt,pagetxt;
    String bookname="";
    String filepath="/sdcard/Download/";
    ProcessText processText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        initView();
        setListener();
    }

    private void setListener() {
        View.OnClickListener listener=new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String content="";
                switch (v.getId()){
                    case R.id.getPre:
                        content=processText.getPre();
                        readtxt.setText(content);
                        break;

                    case R.id.getNext:
                       content=processText.getNext();
                       readtxt.setText(content);
                       getPage();
                        break;
                }
            }
        };
        getNext.setOnClickListener(listener);
        getPre.setOnClickListener(listener);
    }
    private void initView() {
        scroll=findViewById(R.id.scroll);
        getPre=findViewById(R.id.getPre);
        getNext=findViewById(R.id.getNext);
        pagetxt=findViewById(R.id.pagetxt);
        readtxt = findViewById(R.id.readtxt);
        Intent it=getIntent();
        bookname=it.getStringExtra("bookname");
        String content = readAssetsTxt();
        File file=writeTxtToFilr(content,filepath,bookname);
        Log.e("iffile", String.valueOf(file==null));
        processText=new ProcessText(file,0);
        content=processText.getNext();

        readtxt.setText(content);
        getPage();
    }

    private void getPage() {
        pagetxt.setText("第"+processText.currentpage+"页/共"+processText.pages+"页");
        scroll.scrollTo(0,0);
    }

    private File writeTxtToFilr(String content, String filepath, String bookname) {
        File file=new File(filepath+bookname);
        try{
            FileOutputStream fos=new FileOutputStream(file,false);
            //文件输出流对于不存在的文件会创建， false不追加，也是覆盖
            fos.write(content.getBytes());
            fos.close();
            return file;
        }catch (FileNotFoundException e){
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


        return null;
    }

    private String readAssetsTxt() {
        try {
            //File file=new File(filepath+bookname+".txt");
           // FileInputStream is=new FileInputStream(file);
            InputStream is = getAssets().open(bookname+".txt");
            int size = is.available();
            byte[] buffer = new byte[size];
            is.read(buffer);
            is.close();
            String text = new String(buffer, "utf-8");
            return text;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
